package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.mall.product.converter.dto.PlatformAttrConverter;
import com.cskaoyan.mall.product.dto.PlatformAttrInfoDTO;
import com.cskaoyan.mall.product.dto.PlatformAttrValueDTO;
import com.cskaoyan.mall.product.mapper.PlatformAttrInfoMapper;
import com.cskaoyan.mall.product.mapper.PlatformAttrValueMapper;
import com.cskaoyan.mall.product.model.PlatformAttrInfo;
import com.cskaoyan.mall.product.model.PlatformAttrValue;
import com.cskaoyan.mall.product.query.PlatformAttrInfoParam;
import com.cskaoyan.mall.product.query.PlatformAttrValueParam;
import com.cskaoyan.mall.product.service.PlatformAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author Macro_Ray
 * @since 2024/6/7 上午9:40
 */
@Service
public class PlatformAttrServiceImpl implements PlatformAttrService {

    @Autowired
    PlatformAttrConverter platformAttrConverter;
    @Autowired
    PlatformAttrInfoMapper platformAttrInfoMapper;
    @Autowired
    PlatformAttrValueMapper platformAttrValueMapper;

    @Override
    public List<PlatformAttrInfoDTO> getPlatformAttrInfoList(Long firstLevelCategoryId, Long secondLevelCategoryId,
                                                             Long thirdLevelCategoryId) {

        List<PlatformAttrInfo> platformAttrInfoList =
                platformAttrInfoMapper.selectPlatFormAttrInfoList(firstLevelCategoryId, secondLevelCategoryId,
                        thirdLevelCategoryId);
        return platformAttrConverter.platformAttrInfoPOs2DTOs(platformAttrInfoList);
    }

    @Override
    public void saveAttrInfo(PlatformAttrInfoParam platformAttrInfoParam) {
        // 现在接收的是一个从前端传过来的VO对象数据，需要先把他转化成PO对象，然后再保存到数据库 PlatformAttrInfo 表中
        PlatformAttrInfo platformAttrInfo = platformAttrConverter.platformAttrInfoParam2Info(platformAttrInfoParam);
        // 判断属性信息是否存在
        if (platformAttrInfo.getId() == null) {
            // 新增属性信息
            platformAttrInfoMapper.insert(platformAttrInfo);
        } else {
            // 更新属性信息
            platformAttrInfoMapper.updateById(platformAttrInfo);
        }

        // todo 为什么要先删除再添加
        // platformAttrValue平台属性值，先删除，在新增的方式！
        LambdaQueryWrapper<PlatformAttrValue> platformAttrValueQueryWrapper = new LambdaQueryWrapper<>();
        // 删除平台属性原本在数据库中对应的属性值
        platformAttrValueQueryWrapper.eq(PlatformAttrValue::getAttrId, platformAttrInfo.getId());
        platformAttrValueMapper.delete(platformAttrValueQueryWrapper);

        // 获取页面传递过来的所有平台属性值数据
        List<PlatformAttrValue> platformAttrValueList = platformAttrInfo.getAttrValueList();

        // todo CollectionUtils是什么
        if (!CollectionUtils.isEmpty(platformAttrValueList)) {
            // 循环遍历
            for (PlatformAttrValue platformAttrValue : platformAttrValueList) {
                // 获取平台属性Id 给attrId
                platformAttrValue.setId(null);
                platformAttrValue.setAttrId(platformAttrInfo.getId());
                platformAttrValueMapper.insert(platformAttrValue);
            }
        }
    }

    @Override
    public List<PlatformAttrValueDTO> getPlatformAttrValueList(Long attrId) {
        LambdaQueryWrapper<PlatformAttrValue> platAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 创建 PlatformAttrValue表中和请求参数attrId匹配的 查询条件
        platAttrValueLambdaQueryWrapper.eq(PlatformAttrValue::getAttrId, attrId);
        List<PlatformAttrValue> platformAttrValueList = platformAttrValueMapper.selectList(platAttrValueLambdaQueryWrapper);
        return platformAttrConverter.platformAttrValuePOs2DTOs(platformAttrValueList);
    }

}

